package dao

import (
	"context"
	"fmt"
	"github.com/olivere/elastic/v7"
	"github.com/tidwall/gjson"
	"go.uber.org/zap"
	"kee/utils/zaplog"
)

// 添加一条数据
func AddStockData(ctx context.Context, client *elastic.Client, indexName string, stockData *string) error {

	exists := gjson.Get(*stockData, "new_price").Exists()
	if !exists {
		zaplog.Trace("esExists").Error("AddStockData", zap.String("stockData", *stockData))
		return nil
	}

	_, err := client.Index().Index(indexName).BodyString(*stockData).Do(ctx)
	if err != nil {
		return fmt.Errorf("AddStockData|err=%v", err)
	}

	return nil

}
